package hw2.jpa.ejb.service;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import hw2.jpa.ejb.model.Contract;
import hw2.jpa.ejb.model.Product;
import hw2.jpa.ejb.service.remote.RevenueRecognizerRemote;

@Stateless
public class RevenueRecognizer implements RevenueRecognizerRemote {

	@PersistenceContext(unitName = "RevenueRecognitionEJB")
	private EntityManager entityManager;

	@Override
	public long createProduct(Product product) {
		entityManager.persist(product);
		entityManager.flush();
		return product.getId();		
	}

	@Override
	public Product findProduct(Long id) {
		return (Product) entityManager.find(Product.class, id);
	}

	@Override
	public long createContract(Contract contract) {
		entityManager.persist(contract);
		entityManager.flush();
		return contract.getId();
	}

	@Override
	public Contract findContract(Long id) {
		return (Contract) entityManager.find(Contract.class, id);
	}

}
